define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/highsea/index' + location.search,
                    // add_url: 'business/highsea/add',
                    apply_url: 'business/highsea/apply',
                    allot_url: 'business/highsea/allot',
                    // edit_url: 'business/highsea/edit',
                    del_url: 'business/highsea/del',
                    // multi_url: 'business/highsea/multi',
                    // import_url: 'business/highsea/import',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'gender', title: __('Gender'), searchList: { "0": __('Gender 0'), "1": __('Gender 1'), "2": __('Gender 2') }, formatter: Table.api.formatter.normal },
                        { field: 'source.name', title: __('Sourceid') },
                        { field: 'deal', title: __('Deal'), searchList: { "0": __('Deal 0'), "1": __('Deal 1') }, formatter: Table.api.formatter.normal },
                        // { field: 'admin.nickname', title: __('Adminid') },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [ //增加自定义按钮组
                                {
                                    name: 'apply', //按钮名字 给html页面中 data-operate-apply 要一样
                                    title: '领取', //提示内容
                                    icon: 'fa fa-plus',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax', //btn-ajax有这个class就会异步请求
                                    url: $.fn.bootstrapTable.defaults.extend.apply_url,
                                    confirm: '是否确认领取客户资源',
                                    extend: "data-toggle='tooltip'",
                                    success: function (data, ret) {
                                        //成功后刷新表格
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'allot',
                                    title: '分配',
                                    icon: 'fa fa-share',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-success btn-xs btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.allot_url,
                                }
                            ]

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //给工具栏上面的按钮增加绑定事件
            $('.btn-apply').on('click', function () {
                //获取表格中复选框选中id
                var ids = Table.api.selectedids(table);

                //弹出确认对话框
                layer.confirm('是否确认领取客户资源', { title: '领取', btn: ['是', '否'] }, function (index) {

                    //关闭窗口
                    layer.close(index)

                    //发送ajax
                    $.post($.fn.bootstrapTable.defaults.extend.apply_url, { ids: ids }, function (success) {
                        if (success.code) {
                            Toastr.success(success.msg)
                            //刷新表格的意思
                            table.bootstrapTable('refresh');
                        } else {
                            Toastr.error(success.msg)
                        }

                    }, 'json')
                })
            })

            $('.btn-allot').on('click', function () {
                //获取表格中复选框选中id
                var ids = Table.api.selectedids(table);

                // 打开一个界面
                Fast.api.open($.fn.bootstrapTable.defaults.extend.allot_url + `?ids=${ids}`, '分配')
            })

        },

        // recyclebin: function () {
        //     // 初始化表格参数配置
        //     Table.api.init({
        //         extend: {
        //             'dragsort_url': ''
        //         }
        //     });

        //     var table = $("#table");

        //     // 初始化表格
        //     table.bootstrapTable({
        //         url: 'business/highsea/recyclebin' + location.search,
        //         pk: 'id',
        //         sortName: 'id',
        //         columns: [
        //             [
        //                 { checkbox: true },
        //                 { field: 'id', title: __('Id') },
        //                 {
        //                     field: 'deletetime',
        //                     title: __('Deletetime'),
        //                     operate: 'RANGE',
        //                     addclass: 'datetimerange',
        //                     formatter: Table.api.formatter.datetime
        //                 },
        //                 {
        //                     field: 'operate',
        //                     width: '140px',
        //                     title: __('Operate'),
        //                     table: table,
        //                     events: Table.api.events.operate,
        //                     buttons: [
        //                         {
        //                             name: 'Restore',
        //                             text: __('Restore'),
        //                             classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
        //                             icon: 'fa fa-rotate-left',
        //                             url: 'business/highsea/restore',
        //                             refresh: true
        //                         },
        //                         {
        //                             name: 'Destroy',
        //                             text: __('Destroy'),
        //                             classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
        //                             icon: 'fa fa-times',
        //                             url: 'business/highsea/destroy',
        //                             refresh: true
        //                         }
        //                     ],
        //                     formatter: Table.api.formatter.operate
        //                 }
        //             ]
        //         ]
        //     });

        //     // 为表格绑定事件
        //     Table.api.bindevent(table);
        // },

        add: function () {
            Controller.api.bindevent();
        },
        allot: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
